$(function() {
	initBlockUI();
	updateTable(0, "#pager");
	initPagination();
});

// 初始化分页
function initPagination()
{
	$("#pager").pagination($("#wholepage").val(), {
		items_per_page: 10,
		prev_text: "前一页",
		next_text: "后一页",
		callback: updateTable
	});
}

// 更新表格
function updateTable(page, pager_block)
{
	var url = "position_allocate.php";
	var pars = {
		page_status: 1,
		page: (page + 1)
	};
	$.post(url, pars, function(xml) {
		$("#tbody2").html("");
		$("item", xml).each(function(i) {
			var id = $("id", this).text();
			var username = $("username", this).text();
			var positionid = $("positionid", this).text();
			var regtime = $("regtime", this).text().split(" ")[0];
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>").html(username))
				.append($("<td></td>").html(regtime))
				.append($("<td></td>").html($("<a href=\"#\"></a>")
					.attr("userid", id)
					.attr("posid", positionid)
					.attr("id", "allocate_" + id)
					.html("分配")
					.click(function() {
						showPositionChange(this);
						return false;
					})
				))
				.append($("<td></td>").attr("id", "status_" + id))
			.appendTo("#tbody2");
		});
		tablePretty("#tbody2");
	});
	return false;
}

// 显示更改框
function showPositionChange(block)
{
	var id = $(block).attr("userid");
	var posid = $(block).attr("posid");
	var username = $(block).parent().parent("tr").find("td").eq(0).html();
	var str = "分配 " + username + " 的职位：";
	$("#modify_header").html(str);
	getCompanyListViaJs("#company_select", -1);
	$("#company_select").change(function() {
		getSectionListViaJs("#section_select", $(this).val(), -1);
	});
	$("#section_select").change(function() {
		getPositionListViaJs("#position_select", $(this).val(), posid);
	});
	$("#modify_button").click(function() {
		// chPosition(id, username, $("#position_select").val());
		var posname = $("#position_select > option:selected").html();
		var str = ["已将职位 " + posname + " 分配给 " + username + " ！", "职位分配失败！"];
		var url = "position_allocate.php";
		var pars = { page_status: 2, id: id, position_id: $("#position_select").val() };
		$.post(url, pars, function(text) {
			if (/^Success/.test(text)) {
				$("#status_" + id).html($("<span></span>").css({color: "green"}).html(str[0]).fadeOut(6000));
				$("#allocate_" + id).attr("posid", pars["position_id"]);
				$.modal.close();
			}
			else { $("#status_" + id).html($("<span></span>").css({color: "red"}).html(str[1]).fadeOut(6000)); $.modal.close(); }
		});
	});
	$("#position_modify").modal();
}

// 更改员工职位
function chPosition(id, username, posid)
{
	var posname = $("#position_select > option:selected").html();
	var str = ["已将职位 " + posname + " 分配给 " + username + " ！", "职位分配失败！"];
	var url = "position_allocate.php";
	var pars = { page_status: 2, id: id, position_id: posid };
	$.post(url, pars, function(text) {
		if (/^Success/.test(text)) { $("#status_" + id).html($("<span></span>").css({color: "green"}).html(str[0]).fadeOut(6000)); $.modal.close(); }
		else { $("#status_" + id).html($("<span></span>").css({color: "red"}).html(str[1]).fadeOut(6000)); $.modal.close(); }
	});
}

// 更改员工职位
function chPosition1(id)
{
   var url = "position_allocate.php";
   var pars = 
     {
	"id": id,
	"page_status": 2,
	  "position_id": $F("chposition_" + id)
     };
   var myAjax = new Ajax.Request(
       url,
       {
	  method: "post",
	    parameters: pars,
	    onComplete: function(req)
	      {
		 $("status_" + id).update((req.responseText.include("success")) ? "<font color=\"green\">更改员工职位成功！</font>" : "<font color=\"red\">更改员工职位失败！</font>");
	      }
       }
   );
}
